package com.blog.controller.admin;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.Blogger;
import com.blog.service.BloggerService;
import com.blog.util.UtilDate;
import com.blog.util.MD5;
import com.blog.util.UtilResponse;
/**
 * 管理员博主控制层
 * @author liuxing
 *
 */
@Controller
@RequestMapping("/admin/blogger")
public class BloggerAdminController {
	
	@Resource
	private BloggerService bloggerService;
	
	/**
	 * 查询博主信息
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/find")
	public String find(HttpServletResponse response) throws Exception{
		//查询
		Blogger blogger = bloggerService.find();
		//JSON
		JSONObject resule = (JSONObject) JSONObject.toJSON(blogger);
	/*	JSONObject resule = new JSONObject();
		resule.put("blogger", blogger);
	    2种方式皆可以                           
	 */	//返回结果
		UtilResponse.write(response, resule);
		return null;
	}
	
	/**
	 * 修改博主密码
	 * @param id
	 * @param password 原密码
	 * @param newPassword 前端加密
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyPassword")
	public String modifyPassword(int id,String password,String newPassword,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		//查询博主信息
		Blogger blogger = bloggerService.find();
		//对原密码进行判断
		String md5 = MD5.GetMD5Code(blogger.getUserName()+password);
		if(md5.equals(blogger.getPassword())){
			result.put("success", false);
			UtilResponse.write(response, result);
			return null;
		}
		blogger.setPassword(password);
		int isok = bloggerService.update(blogger);
		if(isok>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 保存博主信息
	 * @param imageFile  图片
	 * @param blogger
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public String save(MultipartFile imageFile,Blogger blogger,HttpServletRequest request,HttpServletResponse response) throws Exception{
		//上传的图片重新命名并保存
		if(!imageFile.isEmpty()){
			String filePath=request.getServletContext().getRealPath("/");
			String imageName=UtilDate.getCurrentDateStr()+"."+imageFile.getOriginalFilename().split("\\.")[1];
			imageFile.transferTo(new File(filePath+"static/userImages/"+imageName));
			//重置图片
			blogger.setImageName(imageName);
		}
		int isok = 0;//操作结果
		//判断查询类型
		if(blogger.getId() == null){
			isok = bloggerService.add(blogger);//添加
		}else{
			isok = bloggerService.update(blogger);//修改
		}
		JSONObject result = new JSONObject();
		if(isok > 0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
	//直接返回弹出框
	/*	if(isok>0){
			result.append("<script language='javascript'>alert('修改成功！');</script>");
		}else{
			result.append("<script language='javascript'>alert('修改失败！');</script>");
		}*/
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 删除博主信息
	 * @param ids
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(String ids,HttpServletResponse response) throws Exception{
		String idsStr[] = ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			bloggerService.delete(Integer.parseInt(idsStr[i]));
		}	
		JSONObject result = new JSONObject();
		result.put("success", true);
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 管理员退出注销
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/logout")
	public String  logout()throws Exception{
		SecurityUtils.getSubject().logout();
		return "redirect:/login.jsp";
	}
}
